package com.sun.alen.controller;

import com.sun.alen.entity.*;
import com.sun.alen.service.StudentService;
import com.sun.alen.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/student")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;
    /**
     * 学生登录
     * @param student
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Student student){
        Student student1=studentService.login(student);
        if (student1!=null){
            //登录成功，生成令牌，下发令牌
            Map<String,Object> claims=new HashMap<>();
            claims.put("student",student1);
            String jwt= JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }else {
            return Result.error("登录失败");
        }
    }

    /**
     * 获取所有学生
     * @return
     */
    @GetMapping("/all")
    public Result all(){
        List<Student> studentList=studentService.all();
        return Result.success(studentList);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Student student){
        boolean flag = studentService.add(student);
        if (flag){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        boolean flag = studentService.update(student);
        if (flag){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 根据ID删除学生
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(String id){
        boolean flag=studentService.delete(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 搜索学生
     * @param input
     * @param select
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestParam(name = "input", required = false, defaultValue = "") String input,
                         @RequestParam(name = "select", required = false, defaultValue = "1") String select,
                         @RequestParam(name = "page", required = false, defaultValue = "1") Integer currentPage,
                         @RequestParam(name = "size", required = false, defaultValue = "5") Integer pageSize){
        PageBean pageBean = studentService.search(input, select, currentPage, pageSize);
        return Result.success(pageBean);
    }

    @PutMapping("/repass")
    public Result repass(@RequestParam("phone") String phone,
                         @RequestParam("pass") String pass,
                         @RequestParam("verify") String verify) {
        log.info("{}", phone);
        log.info("{}", pass);
        log.info("{}", verify);
        int flag = studentService.repass(phone, pass, verify);
        if (flag == 0) {
            return Result.success();
        } else if (flag == 1) {
            return Result.error("验证码错误");
        } else {
            return Result.error("重置失败");
        }
    }

    @GetMapping("/searchTeacher")
    public Result searchTeacher(@RequestParam(name = "input",required = false,defaultValue = "")String input,
                         @RequestParam(name = "select",required = false,defaultValue = "1")String select,
                         @RequestParam(name = "teacherId",required = false,defaultValue = "")Integer teacherId,
                         @RequestParam(name = "page",required = false,defaultValue = "1")Integer currentPage,
                         @RequestParam(name = "size",required = false,defaultValue = "5")Integer pageSize){
        PageBean pageBean=studentService.searchTeacher(input,select,teacherId,currentPage,pageSize);
        return Result.success(pageBean);
    }


    @GetMapping("/fetch")
    public Result getTeacherByPro(String id){
        return Result.success(studentService.getStudentByMain(id));
    }


    /**
     * 获取jwt令牌
     * @param authorizationHeader
     * @return
     */
    @GetMapping("/jwt")
    public Result jwt(@RequestHeader("Authorization")String authorizationHeader){
        String token= authorizationHeader.substring("Bearer".length()).trim();
        //验证JWT令牌
        Claims claims= JwtUtils.parseJWT(token);
        return Result.success(claims);
    }

    /**
     * 修改密码
     * @param student
     * @return
     */
    @PutMapping("/password")
    public Result updatePassword(@RequestBody Student student){
        boolean flag=studentService.updatePassword(student);
        Student student1=studentService.selectById(student.getStudentId());
        if (flag){
            return Result.success(student1);

        }else {
            return Result.error("修改失败");
        }
    }
}
